#!coding=utf-8

from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    driver.find_element_by_name("管理").click()
    driver.find_element_by_name("安装包管理").click()
    time.sleep(1)
    try:
        #如果找到有安装包存在，点击选中所有，再一键清理
        driver.find_element_by_id("cn.goapk.market:id/local_pg_check").click()
        driver.find_element_by_id("cn.goapk.market:id/local_pg_delete_bt").click()
        driver.implicitly_wait(5)
        driver.find_element_by_name("确定").click()
        driver.back()
    except:
        driver.back()
    driver.find_element_by_name("首页").click()


def clickappdown(driver):
    # time.sleep(2)
    # swipeUp(driver,0.9,random.randint(0,8)*0.1)
    # time.sleep(2)
    try:
        btns=WaitforElementsById(driver,'cn.goapk.market:id/grp_body',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        for n in [0,1]:
            try:
                size=WaitforElementById(btns[n],"cn.goapk.market:id/txt_center_info_without_img",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size
            if 'G' not in size:
                if 'K' in size or float(size[0:-1])<20:
                    continue
            try:
                down_btn=WaitforElementById(btns[n],'cn.goapk.market:id/grp_op',30)
            except NoSuchElementException:
                driver.back()
                break
            else:
                down_btn.click()
                try:
                    cancel=WaitforElementByName(driver,u'取消',5)
                except NoSuchElementException:
                    pass
                else:
                    cancel.click()
                waittime=20
                while waittime:
                    try:
                        text=WaitforElementById(btns[n],'cn.goapk.market:id/center_progress_right_info',10).get_attribute("text")
                    except NoSuchElementException:
                        pass
                    print text
                    if '0B/S' not in text:
                        down_btn.click()
                        time.sleep(1)
                        btns[n].click()
                        try:
                            delet_btn=WaitforElementById(driver,'cn.goapk.market:id/btn_cancel',30)
                        except NoSuchElementException:
                            raise NoSuchElementException
                        else:
                            delet_btn.click()
                            try:
                                bar=WaitforElementById(driver,'cn.goapk.market:id/dlg_button_bar',10)
                                confirm=WaitforElementByName(bar,u'确定',10)
                            except NoSuchElementException:
                                raise NoSuchElementException
                            else:
                                confirm.click()

                        break
                    else:
                        time.sleep(0.5)
                        if waittime==0:
                            down_btn.click()
                            time.sleep(1)
                            btns[n].click()
                            try:
                                delet_btn=WaitforElementById(driver,'cn.goapk.market:id/btn_cancel',30)
                            except NoSuchElementException:
                                raise NoSuchElementException
                            else:
                                delet_btn.click()
                                try:
                                    bar=WaitforElementById(driver,'cn.goapk.market:id/dlg_button_bar',10)
                                    confirm=WaitforElementByName(bar,u'确定',10)
                                except NoSuchElementException:
                                    raise NoSuchElementException
                                else:
                                    confirm.click()
                            break
                        waittime=waittime-1
                print "----------------------------------------------"
                driver.back()
    driver.back()

def soft(driver):
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    applistcategory = [u'金融理财',u'系统工具',u'手机安全',u'浏览器',u'输入法',u'音乐音频',u'主题桌面',u'视频播放',u'摄影美化',u'气象交通',u'购物支付', \
                       u'综合服务',u'通信聊天',u'社交网络',u'新闻阅读',u'办公学习',u'阅读器']
    try:
        WaitforElementByName(driver,u'应用',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    try:
        close_btn=WaitforElementById(driver,'cn.goapk.market:id/btn_close',10)
    except NoSuchElementException:
        pass
    else:
        close_btn.click()
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in applistcategory:
        try:
            close_btn=WaitforElementById(driver,'cn.goapk.market:id/btn_close',10)
        except NoSuchElementException:
            pass
        else:
            close_btn.click()
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                WaitforElementByName(driver,u'全部',30).click()
            except NoSuchElementException:
                driver.back()
                continue
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()
def game(driver):
    applistcategory = [u'网络游戏',u'创意休闲',u'动作射击',u'益智棋牌',u'角色扮演',u'体育竞速',u'模拟游戏',u'策略经营',u'竞技飞行']
    try:
        WaitforElementByName(driver,u'游戏',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException

    try:
        close_btn=WaitforElementById(driver,'cn.goapk.market:id/btn_close',10)
    except NoSuchElementException:
        pass
    else:
        close_btn.click()

    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in applistcategory:
        try:
            close_btn=WaitforElementById(driver,'cn.goapk.market:id/btn_close',10)
        except NoSuchElementException:
            pass
        else:
            close_btn.click()
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                WaitforElementByName(driver,u'全部',30).click()
            except NoSuchElementException:
                driver.back()
                continue
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()
def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION             # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'cn.goapk.market'      # apk的包名
    desired_caps['appActivity'] = 'com.anzhi.market.ui.MainActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    #del_appdown_task(driver)
    soft(driver)
    game(driver)
    driver.quit()

#run()